package com.lengxf.pay.web.common;

import com.lengxf.pay.starter.parent.bean.PayBaseResult;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class R<T> {

    private int code;

    private String msg;

    private T data;

    public static <T> R<T> success() {
        return restResult(null, ErrorCode.SUCCESS.getCode(), "");
    }

    public static <T> R<T> success(T data) {
        return restResult(data, ErrorCode.SUCCESS.getCode(), "");
    }

    public static <T> R<T> success(T data, String msg) {
        return restResult(data, ErrorCode.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> success(T data, String msg, ErrorCode errorCode) {
        return restResult(data, errorCode.getCode(), msg);
    }

    public static <T> R<T> success(T data, ErrorCode errorCode) {
        return restResult(data, errorCode.getCode(), errorCode.getMsg());
    }


    public static <T> R<T> fail() {
        return restResult(null, ErrorCode.FAIL.getCode(), "");
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, ErrorCode.FAIL.getCode(), msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, ErrorCode.FAIL.getCode(), "");
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, ErrorCode.FAIL.getCode(), msg);
    }

    public static <T> R<T> fail(T data, String msg, ErrorCode errorCode) {
        return restResult(data, errorCode.getCode(), msg);
    }

    public static <T> R<T> fail(T data, ErrorCode errorCode) {
        return restResult(data, errorCode.getCode(), errorCode.getMsg());
    }


    public static <T> R<T> restResult(T data, int code, String msg) {
        return new R<T>().setData(data)
                .setCode(code).setMsg(msg);
    }

    public static <T> R<T> convert(PayBaseResult payBaseResult) {
        if(!payBaseResult.getSuccess()){
            return R.fail(payBaseResult.getOutMsg());
        }
        return R.success();
    }

}
